package com.xhsj.user.hr.process.message.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhsj.user.hr.process.message.entity.TbProjectMessage;
import com.xhsj.user.hr.process.message.service.TbProjectMessageService;
import com.xhsj.user.utils.Message;
import com.xhsj.user.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * <p>
 * 项目消息 前端控制器
 * </p>
 *
 * @author suxiaolin
 * @since 2020-04-01
 */
@Slf4j
@Api(tags = "项目消息")
@RestController
@RequestMapping("/message/tbProjectMessage")
@CrossOrigin(origins = "*", maxAge = 3600)
public class TbProjectMessageController {

	@Autowired
    public TbProjectMessageService service;


	@ApiOperation(value = "项目消息,保存/修改", notes = "项目消息,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    //@PreAuthorize("hasAuthority('tb:project:message:edit')")
	public Message save(@RequestBody TbProjectMessage data){
    	try {
    		service.save(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
			return Message.error(1,"保存失败");
		}
	}


	@ApiOperation(value = "已读消息处理", notes = "已读消息处理")
	@RequestMapping(value = "/handleRead", method = RequestMethod.GET)
	public Message handleRead(@RequestParam("id") String id){

		try {
			TbProjectMessage tbProjectMessage = new TbProjectMessage();
			tbProjectMessage.setId(id);
			tbProjectMessage.setStatus(1);
			tbProjectMessage.setReadTime(new Date());
//			service.updateById(tbProjectMessage);
			return Message.success("成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Message.error(1,"修改失败");
		}

	}


	@ApiOperation(value = "项目消息删除", notes = "通过id删除项目消息信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
    //@PreAuthorize("hasAuthority('tb:project:message:delete')")
	public Message delete(@RequestParam(value="id") String id) {
		try {
			service.removeById(id);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
			return Message.error(1,"修改失败");
		}

	}

	@ApiOperation(value = "批量删除", notes = "批量删除。")
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	public Message batchDelete(@RequestBody List<String> list) {
		try {
			service.removeByIds(list);
			return Message.success("成功");
		} catch (Exception e) {
    		log.error("删除失败:{}",e);
			return Message.error(1,"修改失败");
		}
    }

//	@ApiOperation(value = "分页查询项目消息list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
//	@RequestMapping(value="/findList",method = RequestMethod.POST)
//    //@PreAuthorize("hasAuthority('tb:project:message:view')")
//	public Message findList(@RequestBody Map<String,Object> data) {
//        try {
//        	IPage<TbProjectMessage> selectPage = service.selectPage(data);
//			return Message.success("成功",selectPage.getRecords(),selectPage.getTotal());
//        } catch (Exception e) {
//			log.error("查询异常:{}",e);
//			return Message.error(1,"修改失败");
//        }
//	}

//	@ApiOperation(value = "查询项目消息所有数据list", notes = "查询项目消息所有数据list。")
//	@RequestMapping(value="/findAll",method = RequestMethod.POST)
//    //@PreAuthorize("hasAuthority('tb:project:message:view')")
//	public Message findAll(){
//		return Message.success("成功",service.selectAll());
//	}


	@ApiOperation(value = "通过id获取项目消息实体类", notes = "通过id获取项目消息实体类。")
	@RequestMapping(value="/getById",method = RequestMethod.GET)
    //@PreAuthorize("hasAuthority('tb:project:message:view')")
	public Message getById(@RequestParam(value="id") String id) {
        return Message.success("成功",service.getById(id));
	}

//	@ApiOperation(value = "通过id获取实体类详细信息", notes = "通过id获取实体类详细信息。")
//	@RequestMapping(value="/detail",method = RequestMethod.GET)
//    //@PreAuthorize("hasAuthority('tb:project:message:view')")
//	public Result detail(@RequestParam(value="id") String id) {
//        return new Result<>(Result.CODE_SUCCESS,"",service.getById(id));
//	}

    @ApiOperation(value = "通过用户id获取详细信息", notes = "通过用户id获取详细信息")
    @RequestMapping(value="/detailByUserId",method = RequestMethod.POST)
    public Message detailByUserId(@RequestBody Map<String,Object> param) {
	    try {
            log.info("通过用户id获取详细信息参数,{}",param);
            if(StringUtils.isNull(param.get("userId")))
				Message.error("当前用户id不能为空");
            if(StringUtils.isNull(param.get("status")))
				Message.error("读取状态不能为空");
            if(StringUtils.isNull(param.get("projectId")))
				Message.error("项目id不能为空");
            int currentPage=1;
            if (!Objects.isNull(param.get("page"))) {
                currentPage=(Integer) param.get("page");
            }
            int pageSize=20;
            if (!Objects.isNull(param.get("size"))) {
                pageSize=(Integer) param.get("size");
            }
            String userId = String.valueOf(param.get("userId"));
            String status = String.valueOf(param.get("status"));
            String projectId = String.valueOf(param.get("projectId"));
            IPage<TbProjectMessage> tbProjectMessageIPage = service.detailByUserId(userId, status, currentPage, pageSize,projectId);
			return Message.success("成功",tbProjectMessageIPage.getRecords());
        }catch (Exception e){
	        log.error("通过用户id获取详细信息错误",e);
			return Message.error(1,"修改失败");
        }

    }


	/**
	 * 给手机App查询消息
	 * @author 苏小林
	 * @date 2021/4/19 16:29
	 * @param: baseParam
	 * @return: com.xhsj.user.utils.Message
	 *
	 */
	@GetMapping("/findAppMessage")
	public Message findAppMessage() {

		try {
			List<Map<String,Object>> list= service.findAppMessage();
			if (list.size() >20) {
				return Message.success("成功",list.subList(1,20));
			}
			return Message.success("成功",list);
		} catch (Exception e) {
			e.printStackTrace();
			return Message.error();
		}
	}

}

